﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoNiuOA.Models;

namespace TaoNiuOA.Web.Controllers
{
    public class DepartmentController : Controller
    {
        private DbEntity db = new DbEntity();

        [IsAdmin]
        public ViewResult Index()
        {
            var list=db.Departments.OrderBy(n=>n.Sort).ToList();
            return View(list);
        }

        [IsAdmin]
        public ActionResult Create()
        {
            return View();
        }

        [IsAdmin]
        [HttpPost]
        public ActionResult Create(Department department)
        {
            if (ModelState.IsValid)
            {
                //判断名称是否已经存在
                foreach (var d in db.Departments)
                {
                    if (d.Name == department.Name)
                    {
                        ViewData["error"] = "该名称已经存在";
                        return View(department);
                    }
                }
                db.Departments.Add(department);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(department);
        }

        [IsAdmin]
        public ActionResult Edit(int id)
        {
            Department department = db.Departments.Find(id);
            return View(department);
        }

        [IsAdmin]
        [HttpPost]
        public ActionResult Edit(Department department)
        {
            if (ModelState.IsValid)
            {
                //判断名称是否已经存在
                foreach (var d in db.Departments)
                {
                    if (d.Name == department.Name && d.DepartmentID != department.DepartmentID)
                    {
                        ViewData["error"] = "该名称已经存在";
                        return View(department);
                    }
                }
                //编辑
                var sql = string.Format("update [departments] set [name]='{0}' where [departmentID]='{1}'",
                    department.Name, department.DepartmentID);
                var n = db.Database.ExecuteSqlCommand(sql);
                return RedirectToAction("Index");
            }
            return View(department);
        }

        [IsAdmin]
        public ActionResult Delete(int id)
        {
            Department department = db.Departments.Find(id);
            return View(department);
        }

        [IsAdmin]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Department department = db.Departments.Find(id);
            db.Departments.Remove(department);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}